package layer.replication;

import layer.versioning.VersionedTuple;
import lights.adapters.Field;
import lights.interfaces.IField;
import lights.interfaces.ITuple;
import lime.AgentLocation;
import lime.ILimeAgent;

public class ReplicableTuple {
	private VersionedTuple versionedTuple;
	private IField isReplica;
	private AgentLocation origCur;
	private AgentLocation origDest;
	
	public IField getIsReplica() {
		return isReplica;
	}

	public void setIsReplica(IField isReplica) {
		this.isReplica = isReplica;
	}

	public ReplicableTuple(){
		isReplica = new Field().setToActual(0);
	}
	
	public ReplicableTuple(ILimeAgent agent, VersionedTuple versionedTuple){
		isReplica = new Field().setToActual(0);
		this.versionedTuple = versionedTuple;
		origCur = new AgentLocation(agent.getMgr().getID());
		origDest = origCur;
	}
	
	public ReplicableTuple(VersionedTuple versionedTuple){
		ITuple tuple = versionedTuple.getTuple();
		// get OrigDest
		origDest = (AgentLocation) tuple.get(tuple.length()-1).getValue();
		tuple = tuple.removeAt(tuple.length()-1);
		// get OrigCur
		origCur = (AgentLocation) tuple.get(tuple.length()-1).getValue();
		tuple = tuple.removeAt(tuple.length()-1);
		// get isReplica
		isReplica = tuple.get(tuple.length()-1);
		tuple = tuple.removeAt(tuple.length()-1);
		versionedTuple.setTuple(tuple);
		this.versionedTuple = versionedTuple;
	}
	
	public VersionedTuple getVersionedTuple() {
		return versionedTuple;
	}
	
	public void setVersionedTuple(VersionedTuple versionedTuple) {
		this.versionedTuple = versionedTuple;
	}
	
	public AgentLocation getOrigCur() {
		return origCur;
	}
	
	public void setOrigCur(AgentLocation origCur) {
		this.origCur = origCur;
	}
	
	public AgentLocation getOrigDest() {
		return origDest;
	}
	
	public void setOrigDest(AgentLocation origDest) {
		this.origDest = origDest;
	}
	
	public boolean isMaster(){
		return isReplica.getValue().equals(new Field().setToActual(0).getValue());
	}
}
